package com.qingzhuge.member.controller;

import com.google.common.collect.Lists;
import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.controller.BasePkLongController;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.member.dto.request.SysImageTextRequestDto;
import com.qingzhuge.member.dto.request.SysImageTextResponseDto;
import com.qingzhuge.member.entity.SysImageText;
import com.qingzhuge.member.service.IMalImageTextService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import org.apache.http.util.TextUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


/**
 * @author : zero.xiao
 * @description : 图文管理
 * @date :2018/6/8 0008 下午 14:49
 * @modified :
 */
@ASysLog(value = "图文管理")
@AResource(name = "图文管理", type = EResourceType.menu, catalog = "会员管理")
@Api(tags = "S图文管理")
@RestController
@RequestMapping("api/sys/auth/image-text")
public class SysImageTextController extends BasePkLongController<
        SysImageText, IMalImageTextService, SysImageTextRequestDto, SysImageTextResponseDto> {

    @Override
    public ResponseEntity<ResponseBodyDto<SysImageTextResponseDto>> save(@RequestBody SysImageTextRequestDto requestDto) {
        if (null == requestDto || TextUtils.isEmpty(requestDto.getMainTitle())
                || TextUtils.isEmpty(requestDto.getDescription())
                || null == requestDto.getShowCategory()) {
            return returnFailed("添加数据不能为空");
        }
        SysImageText imageText = service.dto2Bean(requestDto);
        imageText = service.add(imageText, getUserId());
        SysImageTextResponseDto responseDto = service.bean2Dto(imageText, SysImageTextResponseDto.class);
        return returnSuccess(responseDto);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysImageTextResponseDto>> update(@RequestBody SysImageTextRequestDto entity) {
        if (null == entity) {
            return returnFailed("数据不能为空");
        }
        ResponseEntity<ResponseBodyDto<SysImageTextResponseDto>> update = super.update(entity);
        if (update.getStatusCode() == HttpStatus.OK) {
            SysImageText imageText = service.findById(entity.getId());
            SysImageTextResponseDto responseDto = service.bean2Dto(imageText, SysImageTextResponseDto.class);
            return returnSuccess(responseDto);
        } else {
            return update;
        }
    }


    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysImageTextResponseDto>>> queryPage(@RequestBody(required = false) SysImageTextRequestDto requestDto) {
        SysImageText imageText = service.dto2Bean(requestDto);
        PageUtil<SysImageText> page = service.querys(imageText, false);
        List<SysImageTextResponseDto> content = Lists.newArrayList();
        page.getContent().forEach(bean -> content.add(service.bean2Dto(bean, SysImageTextResponseDto.class)));
        PageUtil<SysImageTextResponseDto> pageUtil = PageUtil.of(page.getTotal(), page.getSize(), page.getPage(), page.getPageTotal(), page.isFirst(), page.isLast(), content);
        return returnSuccess(pageUtil);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysImageTextResponseDto>> get(@PathVariable Long id) {
        if (null == id) {
            return returnFailed("数据不能为空");
        }
        SysImageText imageText = service.getDetails(id, false);
        SysImageTextResponseDto responseDto = service.bean2Dto(imageText, SysImageTextResponseDto.class);
        return returnSuccess(responseDto);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        if (id == null) {
            return returnFailed("数据不能为空");
        }
        SysImageText imageText = service.selectEffectiveById(id, SysImageText.DELETE_NORMEL);
        if (Objects.isNull(imageText)) {
            return returnFailed("数据不存在");
        }
        imageText.setDeleted(SysImageText.DELETE_TRUE);
        imageText = service.save(imageText);
        SysImageTextResponseDto responseDto = service.bean2Dto(imageText, SysImageTextResponseDto.class);
        return returnSuccess(responseDto);
    }
}
